/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.yonyou.portcity.controller;


import com.yonyou.portcity.common.validator.ValidatorUtils;
import com.yonyou.portcity.entity.UserEntity;
import com.yonyou.portcity.service.UserService;
import com.yonyou.portcity.utils.JwtUtils;
import com.yonyou.portcity.utils.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户注册
 *
 * @author Mark sunlightcs@gmail.com
 */
@RequestMapping("/api")
@RestController
@Api(description = "农商城用户管理")
@CrossOrigin
public class AppRegisterController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtils jwtUtils;

    @ApiOperation(value = "商城用户注册接口", notes = "商城用户注册接口")
    @RequestMapping(value = "/v1/register", method = RequestMethod.POST)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(paramType = "query", name = "name", dataType = "String", required = false, value = "用戶名"),
            @ApiImplicitParam(paramType = "query", name = "pwd", dataType = "String", required = false, value = "密码"),
            @ApiImplicitParam(paramType = "query", name = "userType", dataType = "String", required = false, value = "商户类型：buy:买家；saler:卖家"),
            @ApiImplicitParam(paramType = "query", name = "nextpwd", dataType = "String", required = false, value = "确认密码"),
    })
    public WebResult register(@RequestParam(value = "name", required = true) String name,@RequestParam(value = "userType", required = true) String userType, @RequestParam(value = "pwd", required = false) String pwd,@RequestParam(value = "pwd", required = false) String nextpwd) {
        if (StringUtils.isEmpty(name)) {
            return WebResult.error("用户名不能为空!");
        }
        if (StringUtils.isEmpty(pwd)) {
            return WebResult.error("密码不能为空!");
        }
        if (StringUtils.isEmpty(userType)) {
            return WebResult.error("请确认注册商户类型!");
        }
        if (StringUtils.isEmpty(nextpwd)) {
            return WebResult.error("确认密码不能为空!");
        }
        UserEntity user = new UserEntity();
        user.setUsername(name);
        user.setUserType(userType);
        user.setPassword(DigestUtils.sha256Hex(pwd));
        user.setCreateTime(new Date());
        return userService.save(user);
    }
    @ApiOperation(value = "校验是否已存在商户", notes = "校验是否已存在商户")
    @RequestMapping(value = "/v1/checkUser", method = RequestMethod.GET)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(paramType = "query", name = "name", dataType = "String", required = false, value = "用戶名"),
    })
    public WebResult checkUser(@RequestParam(value = "name", required = true) String name) {
        if (StringUtils.isEmpty(name)) {
            return WebResult.error("用户名不能为空!");
        }
        return userService.checkUser(name);
    }
    @ApiOperation(value = "查询用户注册量", notes = "查询用户注册量")
    @RequestMapping(value = "/v1/getRegisterUserInfo", method = RequestMethod.GET)
    public WebResult getRegisterUserInfo() {
        return userService.getRegisterUserInfo();
    }
    /**
     * 登录
     */
    @RequestMapping(value = "/v1/login", method = RequestMethod.POST)
    @ApiOperation(value = "登录", notes = "前端商城登录")
    public WebResult login(@RequestBody UserEntity form){
        //表单校验
        ValidatorUtils.validateEntity(form);

        //用户登录
        long userId = userService.login(form);

        //生成token
        String token = jwtUtils.generateToken(userId);

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());

        return WebResult.success(map);
    }
}



